import { message } from 'antd';
import { getIssProveList, deleteIsssProveData } from '@/services/isseprove';

export default {
  state: {
    formFieldsValue: {},
    expandForm: false,
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
  },
  effects: {
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(getIssProveList, payload);
      const {
        pagerArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'updateList',
        payload: {
          data: [],
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'updateList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    *deleteData({ payload }, { call, put }) {
      const response = yield call(deleteIsssProveData, payload);
      const values = { pagerArg: { currentPage: 1, pageSize: 10 } };
      if (response.success) {
        yield put({
          type: 'fetchList',
          payload: values,
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
  },

  reducers: {
    // 获取列表
    updateList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },

    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // 设置页签状态
    // updateTabListQuery(state, { payload }) {
    //   const { tabList } = state;
    //   tabList.forEach((item, index) => {
    //     if (item.itemCode === payload.activeKey) {
    //       tabList[index].isHasBadge = false;
    //     }
    //   });
    //   return {
    //     ...state,
    //     tabList,
    //     ...payload,
    //   };
    // },
  },
};
